#!/bin/bash
#=========================================${自定义函数}===================================================
SCRIPT=$(basename $0)
SCRIPT=${SCRIPT%.*}
function usage(){
printf "Usage: 
	1.${SCRIPT} -p 默认不写,传pack_out目录下文件
	2.${SCRIPT} -p 阿里签名软件压缩包.zip
	3.${SCRIPT} -p -d 在1.2.的基础上加上 -d ,对应为删除远程文件
	4.${SCRIPT} --ftp ls 版本路径 : 查看远程服务器文件是否存在
	5.${SCRIPT} -p -a filename : 上传额外的客户需求文件或者是修改记录
	6.${SCRIPT} --ftp put 本地地址/xxx.zip  服务器地址/xxx.zip : 单独上传文件到对应位置
	7.${SCRIPT} --ftp get 服务器地址/xxx.zip  本地地址/xxx.zip : 下载服务器软件到本地
版本号:${SCRIPT_VERSION}
"
exit 0
}
function usage_detail(){
printf "详细用法: 
1.${SCRIPT} -p :
	默认不写,传pack_out目录下文件,主动打包pack_out下文件,并且检查是否有T-card包和基础包,三个压缩包一起上传
2.${SCRIPT} -p xxx-sign.zip :
	阿里签名软件压缩包.zip,自动解压压缩包内配置文件,读取参数,然后根据参数判断出正确放置路径
3.${SCRIPT} -p -d :
	在1.2.在后面加上 -d ,对应为删除远程文件,该参数不分位置.默认只能删除对应文件,无法删除空文件夹
4.${SCRIPT} --ftp ls 版本路径 :
	可以查看版本是否已经正确传到了服务器
5.${SCRIPT} -p -a filename :
	在1.2.的后面加上 -a 文件 ,对应为上传额外的客户需求文件或者是修改记录,支持txt或者xls,xlsx
6.${SCRIPT} --ftp put 本地地址/xxx.zip  服务器地址/xxx.zip
	可以单独上传Tcard包或者补传其他符合格式的(zip,txt,xls)文件到服务器对应位置
7.${SCRIPT} --ftp get 服务器地址/xxx.zip  本地地址/xxx.zip
	下载服务器对应文件到本地

Tip :
	如果脚本取到参数为空，将会提醒“请注意,有部分参数未获取到”
	如果远程相对应位置已经有文件，将会提醒是否覆盖
PS:
	上传Tcard包或者target包的方法：
	放到pack_out/对应项目文件夹/下即可，
	脚本搜索的关键字为：
	带sdcard.zip为后缀的压缩包 和 target_files-package.zip的压缩包

版本号:${SCRIPT_VERSION}
"
exit 0
}

#格式化输出
RED="\033[0;31m"
YELLOW="\033[1;33m"
GREEN="\033[0;32m"
NO_COLOR="\033[0m"
BOLD="\033[1m"
UNDERLINE="\033[4m"
warn_debug=false
die() {
	echo -e "==> ${RED}${@}${NO_COLOR}"
	exit 1
}

warn() {
	if [[ "$warn_debug" = true ]]; then
		echo -e "==> ${YELLOW}${@}${NO_COLOR}"
	fi
}

good() {
	echo -e "==> ${GREEN}${*}${NO_COLOR}"
}
#程序异常退出辅助函数
error_clean(){
	good "程序被异常中止!!!"
	good "执行清除缓存文件..."

	#清除动作
	test -f ${globelTable} && rm -rf ${globelTable}
	test -d ${DOWNLOAD_TOOLS} && rm -rf ${DOWNLOAD_TOOLS}
	test -f ${JIRAKEY}.txt && rm -rf rm -rf ${CUR_DIR}/${JIRAKEY}.txt
	test -d ${CUR_DIR}/${ParmTemp} && rm -rf ${CUR_DIR}/${ParmTemp}

	good "清除结束!"
	exit 1
}

#获取key对应value值
function getValue(){
	local file=$1
	local key=$2
	if [ -f "${file}" ];then
		local value=$(grep -v "^\s*#" ${file} | grep ${key} | tail -1 | sed 's#^[^=]*=\(.*\)$#\1#g' | sed 's#[[:space:]]##g' )
	fi
	echo ${value}
}

#==============================================${FTP相关函数}=================================================
#ftp连接
function FTP(){
echo " open ${SERVER}
user ${USER} ${PASSWD}
$@
bye
" | ftp -n 
}
#判断是否有文件
function hasFile(){
	local file=$1
	local result=$(FTP dir $file  | wc -l)
	if [ "${result}" == 1 ];then
		warn "服务器发现文件: $file"
		return 0
	fi
	warn "服务器没有发现文件: $file"
	return 1

}
#判断是否有文件夹
function hasDir(){
	local path=$1
	local result=$(FTP dir ${path} | wc -l)
	if [ "${result}" -lt 2 ];then
		warn "服务器没有${path}文件夹"
		return 1
	fi
	warn "服务器有${path}文件夹"
	return 0
}
#创建长目录文件夹
function mkdirParent(){
	local path=$1
	local all_content=""
	
	for content in $(echo ${path} | sed 's#\/# #g')
	do
		#echo $content
		all_content="${all_content}/$content"
		hasDir ${all_content}
		local result=$?
		if [ "${result}" == 1 ];then
			FTP mkdir ${all_content}
		fi
	done
	unset all_content
}
#删除文件
function deleteFile(){
	local file=$1
	hasFile $path
	local result=$?
	if [ "${result}" == 0 ];then
		good "删除服务器文件:$file"
		FTP delete ${file}
	fi
}
#删除空文件夹
function deleteDir(){
	local path=$1
	hasDir $path
	local result=$?
	if [ "${result}" == 0 ];then
		good "删除服务器文件夹:$path"
		FTP rmdir ${path}
	fi
}
#删除所有文件
function deleteAll(){
	local arg=$1
	local result=$(FTP dir ${arg} | wc -l)
	case "${result}" in
		0)
			good "服务器未找到${arg}"
			;;
		1)
			good "删除文件:${arg}"
			FTP delete ${arg}
			;;
		*)
			#FTP暂时无法删除非空文件夹
			good "无法删除文件夹:${arg}"
			#FTP rm ${arg}
			;;
	esac
}
#上传文件函数
function putFile(){
	FILE_TYPE=(zip rar pac xls xlsx txt)
	local local_file=$1
	local remote_file=$2
	test -f ${local_file} || die "本地没有发现文件:${local_file}"
	if [[ "${FILE_TYPE[@]}" =~ "${remote_file##*.}" ]];then
		#远程文件判断,咨询是否覆盖上传
		hasFile ${remote_file} 
		local result=$?
		if [ "${result}" == 0 ];then
			local ANSWER
			read -p "服务器已经有该文件,是否覆盖?[y/n]" ANSWER
			if [ "${ANSWER}" == "n" ];then
				return
			fi
		fi
		#创建目录树
		if [ "${DEBUG}" != "yes" ];then
			mkdirParent $(dirname ${remote_file})
			#文件传输
			local trueUrl="ftp://${SERVER}${remote_file}"
			good "开始传输:${local_file} ===> ${trueUrl}"
			FTP put ${local_file} ${remote_file}
		fi
	else
		die "服务器不支持上传该类型文件"
	fi

}

#=============================================${强制更新}============================================================
#强制更新函数
function forceUpdate(){
	good "当前版本:${SCRIPT_VERSION}"
	local home_root="/scripts_tools"
	local server_size=$(FTP "ls ${home_root}/${SCRIPT}" | awk -F" " '{print$5}')
	#good ${server_size}
	local local_size=$(ls -l `which autoftp` | awk -F" " '{print$5}')
	#good ${local_size}
	if [ "${local_size}" != "${server_size}" ];then
		good "${SCRIPT}版本有更新"
		git-gerrit update 
		local new_version=$(autoftp -v)
		good "最新${new_version}"
		exit 0
	fi
}


#=============================================${获取参数}============================================================
#获取平台
function getPlatform(){
	PLATFORM=$(basename $(git config remote.origin.url) | awk -F"_" '{print$1}' | tr -d [:alpha:])
	PLATFORM=${PLATFORM:$((-4))}
}
#获取系统
function getSystem(){
	SYSTEM=$(basename $(git config remote.origin.url) | awk -F"_" '{print$2}' | tr -d ".")
}
#获取板卡名
function getDevice(){
	DEVICE=$(basename $(dirname ${PROJ}) | awk -F"_" '{print $1}' )
}
#获取客户名
function getCustom(){
	#set -x 
	local project=$(basename $(dirname ${PROJ}))
	good "项目名:${project}"
	local guess3=$(echo $project | awk -F"_" '{print$3}')
	local guess4=$(echo $project | awk -F"_" '{print$4}')
	local guess5=$(echo $project | awk -F"_" '{print$5}')
	case ${PLATFORM} in 
		6735)
			local network=(mt mf mc pt pf pc at af ac)
			if [[ "${network[@]}" =~ "${guess4}" ]];then
				CUSTOM=${guess5}
			else
				CUSTOM=${guess4}
			fi
			;;
		*)
			CUSTOM=${guess4}
			#客户名预测,优先考虑第四个,如果第四个包含数字则选择第三个
			if [ -n "$(echo ${CUSTOM} | egrep "[0-9]")" ];then
				CUSTOM=${guess3}
			fi
			;;
	esac
	test -z ${CUSTOM} && CUSTOM="null"
	good "CUSTOM=${CUSTOM}"
	#set +x
}

#获取flash大小
#匹配配置文件custom_MemoryDevice.h与代码flash类型文件
function getFlash(){
	#set -x 
	local out_md5=$( md5sum ${MEMORY} | awk '{print$1}')
	local match_file=$(find ${CUR_DIR}/bootable/bootloader/preloader/custom ${CUR_DIR}/mediatek/custom/ ${CUR_DIR}/vendor/mediatek/proprietary/bootable/bootloader/preloader/custom -maxdepth 1 -iname "custom_MemoryDevice.h*" -exec md5sum {} ';' 2>/dev/null | awk "/${out_md5}/ {print\$2}"| tail -1)
	good  "匹配到:$match_file"
	if [[ -f "${match_file}" ]];then
		local file=$(basename ${match_file} 2>/dev/null)
		FLASH=${file:22}
		FLASH=$(echo ${FLASH} | tr -d ".")
	fi
	test -z ${FLASH} && FLASH="null"
	good "FLASH=${FLASH}"
	#set +x
}

#获取分辨率
function getPixel(){
	local proj=${PROJ}
	if [ -f ${proj} ];then
		local width=$(getValue ${proj} LCM_WIDTH)
		local height=$(getValue ${proj} LCM_HEIGHT)
		PIXEL="${width}x${height}"
	fi
	test -z ${PIXEL} && PIXEL="null"
	good "PIXEL=${PIXEL}"


}

#获取modem类型
function getModem(){
	local proj=${PROJ}
	if [ -f "${proj}" ];then
		case ${PLATFORM} in 
			6571)
				#71目前只有TD类型
				MODEM="TD"
				;;
			6572|6580)
				local value=$(grep -v "^\s*#" ${proj} | grep "CUSTOM_MODEM" | tail -1 | sed 's#^[^=]*=\(.*\)$#\1#g'| egrep -o band[0-9] | xargs|sed 's# ##g' )
				MODEM="${value}"
				;;
			*)
				local value=$(grep -v "^\s*#" ${proj} | grep "CUSTOM_MODEM" | tail -1 | sed 's#^[^=]*=\(.*\)$#\1#g' |wc -w)
				case ${value} in
					1)
						MODEM="3M"
						;;
					2)
						MODEM="5M"
						;;
					3)
						MODEM="6M"
						;;
					*)
						warn "modem获取出错."
						MODEM=""
						;;
				esac
				;;
		esac
	fi
	test -z ${MODEM} && MODEM="null"
	good "MODEM=${MODEM}"
}
#获取功放
function getSound(){
	#set -x 
	#USING_EXTAMP_HP
	local project=$(basename $(dirname ${PROJ}))
	case ${PLATFORM} in
		6572|6571)
			local result=$(grep -v "^\s*//"  mediatek/custom/${project}/hal/audioflinger/audio/audio_custom_exp.h misc/${project}/custom/hal/audioflinger/audio/audio_custom_exp.h 2>/dev/null| grep USING_EXTAMP_HP | tail -1 |wc -l)
			if [ ${result} == 1 ];then
				SOUND="K类功放"
			else
				SOUND="D类功放"
			fi
			;;
		*)
			local defconfig=$(find device/*/${project}/*/user_defconfig 2>/dev/null)
			if [ -n "${defconfig}" ];then
				local value=$(getValue ${defconfig} CONFIG_MTK_SPEAKER)
				if [ "${value}" = "y" ];then
					SOUND="D类功放"
				fi
			fi
			test -z ${SOUND} && SOUND="K类功放"
			;;
	esac
	good "SOUND=${SOUND}"
	#set +x 
}
#获取客户型号
function getProduct(){
	local prop=${PROP}
	local value=$(getValue ${prop} "ro.product.model=")
	PRODUCT=${value}
	test -z ${PRODUCT} && PRODUCT="null"
	good "PRODUCT=${PRODUCT}"

}
#获取版本号
function getVersion(){
	local prop=${PROP}
	case ${PLATFORM}_${SYSTEM} in 
		6735_ali320|6735_ali305)
			local value=$(getValue ${prop} ro.xh.display.version)
			;;
		*)
			local value=$(getValue ${prop} ro.product.sw.version)
			;;
	esac
	VERSION=$(echo ${value} | tr "." "-")
	test -z ${VERSION} && VERSION="null"
	good "VERSION=${VERSION}"
}
#获取所有参数
function getAll(){
	getPlatform
	getSystem
	getDevice
	getCustom
	getFlash
	getPixel
	getModem
	getSound
	getProduct
	getVersion
	getSignStatus
}
#打印所有参数
function showAll(){
	echo "==========================="
	eval good "0.平台:\\\t\$${PATH_ARRAY[0]}"
	eval good "1.系统:\\\t\$${PATH_ARRAY[1]}"
	eval good "2.板卡:\\\t\$${PATH_ARRAY[2]}"
	eval good "3.客户:\\\t\$${PATH_ARRAY[3]}"
	eval good "4.FLASH:\\\t\$${PATH_ARRAY[4]}"
	eval good "5.分辨率:\\\t\$${PATH_ARRAY[5]}"
	eval good "6.modem:\\\t\$${PATH_ARRAY[6]}"
	eval good "7.功放:\\\t\$${PATH_ARRAY[7]}"
	eval good "8.客户型号:\\\t\$${PATH_ARRAY[8]}"
	eval good "9.软件版本:\\\t\$${PATH_ARRAY[9]}"
	echo "==========================="
}
#轮询显示所有参数
function queueMessage(){
	local remote_path=$1
	showAll
	if [ -n "$(echo ${remote_path} | egrep "null")" ];then
		good "\033[0;31m请注意,有部分参数未获取到\033[0;0m"
	fi
	read -p "该路径是否正确?如果是直接回车,不是请输入[对应序号.正确参数]:" REPLY
}
#添加jirakey
function addJiraKey(){
	local key
	read -p "请输入该项目对应工程名:" key
	#判断key是否合法

	JIRAKEY=${key}

}


#获取配置文件
function getParm(){
	if [ -n "$1" ];then
		local zip_file=$1
		#如果的zip包,则解压里面的参数文件
		ZIP_FLAG="true"
		PROP=$(unzip -l ${zip_file} | grep -i "build.prop" | awk '{print$4}')
		PROJ=$(unzip -l ${zip_file} | grep -i "projectconfig.mk" | awk '{print$4}')
		MEMORY=$(unzip -l ${zip_file} | grep -i "custom_MemoryDevice.h" | awk '{print$4}')
		MKCONFIG=$(unzip -l ${zip_file} | grep -i ".mk" | awk '{print$4}' | xargs)
		#unzip -o ${zip_file} ${PROP} ${PROJ} ${MEMORY} ${MKCONFIG} -d ${ParmTemp} 2>&1 >/dev/null
		unzip -o ${zip_file} ${PROP} ${MEMORY} ${MKCONFIG} -d ${ParmTemp} 2>&1 >/dev/null

		PROP=${CUR_DIR}/${ParmTemp}/${PROP}
		PROJ=${CUR_DIR}/${ParmTemp}/${PROJ}
		MEMORY=${CUR_DIR}/${ParmTemp}/${MEMORY}
	else
		#获取配置参数文件路径
		ZIP_FLAG="false"
		PROP=$(find ${CUR_DIR}/pack_out -iname build.prop)
		PROJ=$(find ${CUR_DIR}/pack_out -iname projectconfig.mk)
		MEMORY=$(find ${CUR_DIR}/pack_out/ -iname custom_MemoryDevice.h)
	fi
	#good "发现配置文件:"
	good $PROP
	good $PROJ
	good $MEMORY
	#good ${MKCONFIG}

}
#获取是否efuse签名版本
function getSignStatus(){
	#set -x 
	MTK_SEC_BOOT=no
	#good platform=${PLATFORM}_${SYSTEM}
	case ${PLATFORM}_${SYSTEM} in
		6737_m|6580_m)
			local signParmFile="${PROJ}"
			if [ "$(echo ${signParmFile}| grep -o "_SIGN/")" -a "$(find ${CUR_DIR}/pack_out/ -iname "*-sign*")" ];then
				MTK_SEC_BOOT="ATTR_SUSBDL_ENABLE"
			fi
			;;
		*)
			if [ "${ZIP_FLAG}" != "true" ];then 
				MTK_SEC_BOOT=$(find ${CUR_DIR}/pack_out/ -iname  "*.mk" | xargs grep "^MTK_SEC_USBDL" | awk -F '=' '{print $2}' | tr -d " ")
			else
				MTK_SEC_BOOT=$(find ${CUR_DIR}/${ParmTemp}/ -iname "*.mk" | xargs grep "^MTK_SEC_USBDL" | awk -F '=' '{print $2}' | tr -d " ")
			fi

			;;
	esac
	
	if [ "${MTK_SEC_BOOT}" == "ATTR_SUSBDL_ENABLE" ];then
		MTK_SEC_BOOT=yes
	else
		MTK_SEC_BOOT=no
	fi
	#good "MTK_SEC_BOOT=${MTK_SEC_BOOT}"
	#set +x 
}
#获取软件对应下载工具和写号工具
function getDownloadTools(){
	DOWNLOAD_TOOLS="下载工具和写号工具"
	local proj=${PROJ}
	#local proj=$1
	local remotePath="/scripts_tools/DownloadTools"
	local rootDir=$(readlink -f .)
	local globelTable="${rootDir}/FileList.csv"
	local platform=$(basename $(git config remote.origin.url))
	local isSign=${MTK_SEC_BOOT}
	local isC2k=$(getValue ${proj} MTK_C2K_SUPPORT)
	local snMark="SN_Writer"
	local downMark="Download"
	local driverMark="Driver.zip"

	test -d ${DOWNLOAD_TOOLS} && rm -rf ${DOWNLOAD_TOOLS}
	mkdir -p ${DOWNLOAD_TOOLS}
	#空值预警
	test -z "${isSign}" && isSign=no
	test -z "${platform}" && platform=NULL
	test -z "${isC2k}" && isC2k=no
	good "平台:$platform"
	good "签名:$isSign"
	good "C2K:$isC2k"
    #获取对应关系表格
	FTP get ${remotePath}/FileList.csv ${globelTable}
	#查表下载
	for content in $(grep -w ${platform} ${globelTable} )
	do 
		tableSign=$(echo $content | cut -d"," -f 2)
		tableC2k=$(echo $content | cut -d"," -f 3)
		if [ "${tableSign}" == "${isSign}" ]&&[ "${tableC2k}" == "${isC2k}" ];then 
			#good $content ;
			local snWriter=$(echo $content | cut -d"," -f 4 | tr -d '\015') 
			local downTools=$(echo $content | cut -d"," -f 5 | tr -d '\015')
			#格式化工具名称
			formatSnWriter=$(echo "${snWriter}" | sed -e "s/${snMark}/写号工具/g")
			formatDownTools=$(echo "${downTools}" | sed -e "s/${downMark}/下载工具/g")
			formatDriver=$(echo "${driverMark}" | sed -e "s/${driverMark}/驱动程序.zip/g")

			good "snWriter = ${formatSnWriter}"
			good "downTools = ${formatDownTools}"
			good "driver = ${formatDriver}"

			FTP get ${remotePath}/${snWriter} ${rootDir}/${DOWNLOAD_TOOLS}/${formatSnWriter}
			FTP get ${remotePath}/${downTools} ${rootDir}/${DOWNLOAD_TOOLS}/${formatDownTools}
			FTP get ${remotePath}/${driverMark} ${rootDir}/${DOWNLOAD_TOOLS}/${formatDriver}
		fi
	done
	#删除下载表格
	test -f ${globelTable} && rm -rf ${globelTable}
	unset content
}


#=================================================${main}=============================================================
#主函数入口
function main(){
	[ "$DEBUG" != yes ] && forceUpdate
	REPLY="N"
	local zip_file=$1
	test -z "${zip_file}" && test ! -d "pack_out" && die "未发现pack_out目录"
	#获取配置文件
	getParm ${zip_file}

	#获取所有参数
	getAll > /dev/null
	#showAll
	#local remote_path=${HOME_ROOT}/sw_bin_mtk${PLATFORM}/${SYSTEM}/${DEVICE}/${CUSTOM}/${FLASH}/${PIXEL}/${MODEM}/${SOUND}/${PRODUCT}/${VERSION}
	#good ${remote_path}
	#if [ -n "$(echo ${remote_path} | egrep "null")" ];then
	#	good "\033[0;31m请注意,有部分参数未获取到\033[0;0m"
	#fi
	#read -p "该路径是否正确?如果是直接回车,不是请输入[对应序号.正确参数]:" REPLY
	while [[ -n "${REPLY}" ]] 
	do
		local num=${REPLY:0:1}
		local content=${REPLY:2}
		#检查输入合法性
		if [[ ${num} =~ ^[[:digit:]]*$ ]]; then
			eval "${PATH_ARRAY[$num]}=${content}"
		else
			warn "请输入正确序列号...."
		fi
		local remote_path=$(echo ${HOME_ROOT}/sw_bin_mtk${PLATFORM}/${SYSTEM}/${DEVICE}/${CUSTOM}/${FLASH}/${PIXEL}/${MODEM}/${SOUND}/${PRODUCT}/${VERSION} | tr "." "-")
		#循环交互
		queueMessage ${remote_path}
	done
	local trueUrl="ftp://${SERVER}${remote_path}"
	good "最终确认上传路径:${trueUrl}"
	#检测参数不完整退出
	if [ -n "$(echo ${remote_path} | egrep "null")" ];then
		die "警告,部分参数未获取到,又没有被修改,无法上传"
	fi
	#添加jirakey
	while [[ -z "${JIRAKEY}" ]]
	do
		addJiraKey
	done
	#程序异常中止信号捕捉,清除缓存文件
	trap "error_clean" INT

	#上传压缩包
	if [ -z "${zip_file}" ];then
		#修复签名软件镜像名带着sign导致找包失败
		local path=$(dirname $(find ${CUR_DIR}/pack_out/ -iname "system*.img"))
		cd ${path}
		
		local sdcard=$(basename $(find $(pwd) -iname "*sdcard.zip") .zip)
		#由于服务器ftp特性,定向修改"."为"-"
		[ -n "${sdcard}" ] && local remote_sdcard_zip=$(echo ${sdcard}| tr "." "-")
		#删除文件
		if [ "${vdelete}" == yes ];then
			deleteAll ${remote_path}/${VERSION}.zip 
			deleteAll ${remote_path}/target_files-package.zip
			deleteAll ${remote_path}/${remote_sdcard_zip}.zip 
			exit 0
		fi
		#压缩传输软件包
		if [ "${DEBUG}" != "yes" ];then
			good "正在压缩pack_out下面的软件包,请稍等:"
			find . -iname "target_files-package.zip" -prune -o -iname "*sdcard.zip" -prune -o -type f -print | xargs zip -r ${VERSION}.zip
		else
			good "测试模式.模拟压缩包"
			zip -r ${VERSION}.zip *.bin
		fi
		#新增下载工具和写码工具插入
		getDownloadTools
		#插入压缩包
		zip -r ${VERSION}.zip ${DOWNLOAD_TOOLS}/
		#传送压缩包
		putFile ${VERSION}.zip ${remote_path}/${VERSION}.zip

		#传送基础包
		test -f target_files-package.zip && putFile target_files-package.zip ${remote_path}/target_files-package.zip

		#传送T-card包
		[ -n "${sdcard}" ] && test -f "${sdcard}.zip" && putFile "${sdcard}.zip" ${remote_path}/${remote_sdcard_zip}.zip

		#创建jirakey
		touch ${JIRAKEY}.txt
		putFile ${JIRAKEY}.txt ${remote_path}/${JIRAKEY}.txt

		#清除动作
		test -d ${DOWNLOAD_TOOLS} && rm -rf ${DOWNLOAD_TOOLS}
		cd - > /dev/null
	else
		if [ "${vdelete}" == yes ];then
			deleteAll ${remote_path}/$(basename ${zip_file} .zip|tr "." "-").zip
			exit 0
		fi
		#新增下载工具和写码工具
		getDownloadTools
		zip -r ${zip_file} ${DOWNLOAD_TOOLS}/
		#传送指定压缩包
		putFile ${zip_file} ${remote_path}/$(basename ${zip_file} .zip|tr "." "-").zip
		#创建jirakey
		touch ${JIRAKEY}.txt
		putFile ${JIRAKEY}.txt ${remote_path}/${JIRAKEY}.txt
		
		#清除动作
		test -d ${DOWNLOAD_TOOLS} && rm -rf ${DOWNLOAD_TOOLS}
		test -f ${JIRAKEY}.txt && rm -rf rm -rf ${CUR_DIR}/${JIRAKEY}.txt
		test -d ${CUR_DIR}/${ParmTemp} && rm -rf ${CUR_DIR}/${ParmTemp}
	fi
    #追加传送文件
	if [ "${vadd}" == "yes" ];then
		local add_file=$(basename ${addFile})
		test -f "${addFile}" || die "${addFile}文件不存在" && putFile ${addFile} ${remote_path}/${add_file}
	fi
	unset REPLY
}


#==================================================${初始化}===========================================
#初始化变量
CUR_DIR=$(readlink -f .)
LOG_DIR="$CUR_DIR/Log"
SERVER="192.168.2.60"
USER="autoftp"
PASSWD="teksun2016"
#HOME_ROOT="/All_In_One_20160321"
#HOME_ROOT="/AutoFtp_Form20160425"
#HOME_ROOT="/AutoFtp_Form20160614"
#HOME_ROOT="/AutoFtp_Form20160829"
HOME_ROOT="/AutoFtp_Form20161125"
SCRIPT_VERSION="v1.6.1"
ParmTemp="ParmTemp"

PLATFORM=""
SYSTEM=""
DEVICE=""
CUSTOM=""
FLASH=""
PIXEL=""
MODEM=""
SOUND=""
PRODUCT=""
VERSION=""
JIARKEY=""
DEBUG="no"
#参数数组
PATH_ARRAY=(PLATFORM SYSTEM DEVICE CUSTOM FLASH PIXEL MODEM SOUND PRODUCT VERSION)


#=================================${参数选择}============================================

ARGS=$(getopt -o a:b:hpdv -al add,version,test,put,dir,file,cdir,delete,ftp,help,debug -- "$@")

eval set -- "${ARGS}" 
#echo "caicai_debug: $@"
while true  ;do
        case "$1" in
        --test) vtest="yes";;
        --put|-p) vput="yes";;
        --dir) vcheckDir="yes";;
        --file) vcheckFile="yes";;
        --cdir) vmkdir="yes";;
        --delete|-d) vdelete="yes";;
        --version|-v) vVersion="yes";;
        --add|-a) 
			vadd="yes"
			addFile=$2
			shift
			;;
        --ftp) vftp="yes";;
        -b) branch="$2";shift;;
        -h|--help) usage_detail;;
        -debug|--debug) DEBUG=yes;;
        --) arg1="$2"; arg2="$3"; arg3="$4";arg4="$5"; break;;
        esac
shift
done
if [ "${DEBUG}" == "yes" ];then
	good "调试模式:${DEBUG}"
fi
#传送文件
if [ "$vput" == "yes" ];then
	#putFile $arg1 $arg2	
	main $arg1 $arg2
	exit 0
fi
#判断是否存在文件夹
if [ "$vcheckDir" == "yes" -a -n "$arg1"  ];then
	hasDir $arg1
	exit 0
fi
#判断是否存在文件
if [ "$vcheckFile" == "yes" -a -n "$arg1"  ];then
	hasFile $arg1
	exit 0
fi
#创建文件夹路径
if [ "$vmkdir" == "yes" -a -n "$arg1"  ];then
	mkdirParent $arg1
	exit 0
fi

#删除文件
if [ "$vdelete" == "yes" -a -n "$arg1"  ];then
	deleteAll $arg1
	exit 0
fi
#远程调用指令
if [ "$vftp" == "yes" ];then
	echo $@
	FTP $arg1 $arg2 $arg3
	exit 0
fi

#内调函数
if [ "$vtest" == "yes" ];then
	echo $@
	$arg1 $arg2 $arg3 $arg4
	exit 0
fi
#版本号打印
if [ "$vVersion" == "yes" ];then
	echo "版本号:${SCRIPT_VERSION}"
	exit 0
fi
#============================================================
#main $1 $2 $3 $4

usage_detail


